<script setup lang='ts'>
import { nextTick, reactive, ref } from 'vue';
import { systemIpDenyApi as systemApi } from '@/api/controller';
import { ToastError, ToastSuccess, Toastloading } from '@/utils/public';
const formLabelWidth = '120px'
const dataForm = ref()
const $emit = defineEmits(['updateList'])
const action = reactive({
  dialogFormVisible: false,
  disabled: false,
  title: '',
  limit_user:[] as any[]
})
interface formQuert {
  id: number
  ip: string
  limit_id: number
  remark: string
}
let form = reactive({} as formQuert)
const formRules = {
  ip: [{ required: true, message: "请输入IP", trigger: "blur" }],
  limit_id: [{ required: true, trigger: 'blur', message: '请选择账号' }],
  remark: [{ required: true, message: "请输入备注", trigger: "blur" }],
}
const handleSubmit = () => {
  dataForm.value.validate(async (valid: any) => {
    if (!valid) return
    let submitSet = false
    const { id } = form
    // 验证IPv4格式
    const ipv4Regex = /^(\d{1,3}\.){3}\d{1,3}$/
    if (!ipv4Regex.test(form.ip)) {
      ToastError('请输入正确的IPv4格式')
      return
    }
    // 验证每个数字在0-255范围内
    const nums = form.ip.split('.')
    for (const num of nums) {
      const n = parseInt(num)
      if (n < 0 || n > 255) {
        ToastError('IP地址每个数字需在0-255范围内')
        return
      }
    }

    const loading = Toastloading()
    if (id) {
      await systemApi.updateSystems(form).then(() => {
        submitSet = true
        ToastSuccess('编辑成功')
      }).catch(() => {
        loading.close()
      })
    } else {
      await systemApi.addSystems(form).then(() => {
        submitSet = true
        ToastSuccess('新增成功')
      }).catch(() => {
        loading.close()
      })
    }
    if (submitSet) {
      loading.close()
      $emit('updateList')
      closeDialog()
    }
  })
}

const userListList = async () => {
    const response = await systemApi.userList();
    const { data = [] } = response
    action.limit_user = data
}

const openDialog = async(query:any = {})=> {
    const loading = Toastloading()
    await userListList();
  const { id = 0, key = '', type = 0, value = '', desc = '' } = query
  if (id) {
    action.title = '编辑配置'
    Object.assign(form, {
      id, key, type, value, desc
    })
  } else {
    action.title = '新增配置'
  }
  loading.close()
  action.dialogFormVisible = true
}

const closeDialog = () => {
  form = reactive({} as formQuert)
  nextTick(() => {
    dataForm.value.clearValidate();
  });
  action.dialogFormVisible = false
}
defineExpose({
  openDialog
})
</script>

<template>
  <div>
    <el-dialog top="10vh" class="xm-dialog input-width" v-model="action.dialogFormVisible"
      :close-on-click-modal="action.disabled" :show-close="action.disabled" :close-on-press-escape="action.disabled"
      width="52%" :title="action.title">
      <el-form ref="dataForm" :model="form" :rules="formRules">
        <el-form-item :label-width="formLabelWidth" label="IP" prop="ip">
          <el-input v-model="form.ip" placeholder="请输入IP" />
        </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="账号" prop="limit_id">
              <el-select v-model="form.limit_id" filterable placeholder="账号">
                <el-option v-for="(user, key) of action.limit_user" :key="key" :label="user.name" :value="user.id"/>
              </el-select>
            </el-form-item>
        <el-form-item :label-width="formLabelWidth" label="备注" prop="remark">
          <el-input v-model="form.remark" :autosize="{ minRows: 3, maxRows: 3 }" type="textarea" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template v-if="!action.disabled" #footer>
        <span class="dialog-footer">
          <el-button @click="closeDialog">取 消</el-button>
          <el-button type="primary" @click="handleSubmit">确 定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>